package io.sdph.core.database;

import io.sdph.core.database.exceptions.DatabaseException;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Manager do komunikacji z bazą danych SQL.
 * 
 * @author Konrad Szewczyk
 * @since 0.0.1
 */
public class DatabaseUtils {

	private static Logger log = Logger.getLogger(DatabaseUtils.class);

	private static String SETTINGS_FILE_NAME = "database_settings";

	private static String PROP_USER = "database.user";
	private static String PROP_PASS = "database.password";
	private static String PROP_URL = "database.url";

	private static String user;
	private static String password;
	private static String url;
	
	private static DatabaseUtils instance;

	private DatabaseUtils() {
		Properties settings = new Properties();
		try {
			/*settings.load(ClassLoader.getSystemResourceAsStream(SETTINGS_FILE_NAME
						+ ".properties"));*/
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			/* user = settings.getProperty(PROP_USER); // "sql426970";
			password = settings.getProperty(PROP_PASS); // "lF4%hL1*"; //
			url = settings.getProperty(PROP_URL); //"jdbc:mysql://sql4.freemysqlhosting.net:3306/sql426970";
			*/
			user = "sql426970";
			password = "lF4%hL1*"; //
			url = "jdbc:mysql://sql4.freemysqlhosting.net:3306/sql426970";
		} catch (InstantiationException e) {
			
		} catch (IllegalAccessException e) {
			
		} catch(ClassNotFoundException e) {
			log.error("Cannot establish database class.", e);
		}
	}
	
	public static DatabaseUtils getInstance() {
		if (instance == null) {
			instance = new DatabaseUtils();
		}
		return instance;
	}
	/*
	 * public Object getObject(String id, Class className) throws
	 * DatabaseException { try { Object obj =
	 * className.getDeclaredConstructor(String.class).newInstance(id);
	 * Connection con = getConnection();
	 * 
	 * return obj; } catch (Exception e) { throw new DatabaseException(e); } }
	 */

	/**
	 * Metoda ustanawia połączenie z bazą danych MySQL.
	 * 
	 * @return uchwyt do połączenia
	 * @throws SQLException
	 * @throws DatabaseException
	 */
	public Connection getConnection() throws DatabaseException {

		Connection conn = null;

		Properties connectionProps = new Properties();
		connectionProps.put("user", user);
		connectionProps.put("password", password);
		try {
			conn = DriverManager.getConnection(url, connectionProps);

			if (log.isDebugEnabled()) {
				log.debug("Connected to database");
			}
		} catch (NullPointerException e) {
			throw new DatabaseException("Key in settings file not found.", e);
		} catch (SQLException e) {
			throw new DatabaseException(e);
		}

		return conn;
	}

	public void closeConnection(Connection conn) throws DatabaseException {
		if (conn == null) {
			throw new DatabaseException("Cannot close null connection.");
		}
		try {
			conn.close();
		} catch (SQLException e) {
			throw new DatabaseException("Cannot close database connection.", e);
		}
	}
}
